#!/usr/bin/perl
## Use our modules.
use strict;
use Data::Dumper;
$| = 1;

## Parse arguments.
my %ARGS;
my $last_arg;
foreach my $a (@ARGV) {
	my $v = 1;
	unless ($a =~ /^-/) {
		if (length($ARGS{$last_arg}) > 0) {
			$ARGS{$last_arg} .= " ";
		}
		$ARGS{$last_arg} .= $a;
		next;
	} else {
		$a =~ s/^(-+)//;
		if ($a =~ /=/) {
			($a, $v) = split(/=/, $a, 0);
		}
		$v =~ s/^"//g;	$v =~ s/"$//g;
		$ARGS{$a} = $v;
		$last_arg = $a;
	}
}
undef($last_arg);

## Setup globals.
my $PWD = $ENV{PWD};
my $HOME = $ENV{HOME};
my $SVN;	chomp($SVN = `which svn`);
unless (-x $SVN) {
	print "Unable to find SVN using `which`.  Fix this.\n";
	print "Got '$SVN' instead.\n";
	exit;
}
my $foo;

unless ($ARGS{quiet} > 0) {
	## Ensure the user is running this in the project directory.
	print "Is $PWD the current SVN trunk for this project? [Y/n] ";
	chomp($foo = <STDIN>);
	## If they're not in the project directory, ask them for the right directory.
	if ($foo =~ /n/i) {
		print "What is the SVN trunk directory? ";
		chomp($foo = <STDIN>);
		## Make sure the supplied directory exists.
		unless (-d $foo) {
			print "That is not a valid directory.  Exiting.\n";
			exit;
		}
		## Make sure the vimrc file is in the directory they specified.
		##	If it's not, we'll error later anyway.
		unless (-f $foo."/vimrc") {
			print "There is not a '$foo/vimrc' file, this cannot be the correct directory.  Exiting.\n";
			exit;
		}
		## Set our new working directory.
		$PWD = $foo;
	}
}
## If we're not forcing...
unless ($ARGS{force} > 0) {
	## And the ~/.vimrc exists...
	if (-f $HOME."/.vimrc") {
		## Warn them and ask if they want to clobber it.
		print $HOME."/.vimrc exists.  Clobber? [y/N] ";
		chomp($foo = <STDIN>);
		unless ($foo =~ /y/i) {
			print "Not clobbering, exiting...\n";
			exit;
		}
		## We'll do the clobbering automatically later.
	}
}

## Open the original vimrc file from the SVN trunk.
open(READFILE, "<".$PWD."/vimrc") or die("Unable to open $PWD/vimrc!\n$!\n");
my $contents;
## Read in the contents so that we can...
while (<READFILE>) {
	## Do some substitutions.
	s/\%SVN_DIR\%/$PWD/g;
	$contents .= $_;
}
## Closer 'er up!
close(READFILE);

## Write this out to the ~/.vimrc file.
open(WRITEFILE, ">".$HOME."/.vimrc") or die("Unable to open $HOME/.vimrc!\n$!\n");
print WRITEFILE $contents;
close(WRITEFILE);

## Placeholder.
my $create_file = 0;
## Unless we're forcing...
unless ($ARGS{force} > 0) {
	## Ask if they'd like the crontab file.
	print "Would you like to create/update the $HOME/.vim_update.sh file for crontab? [Y/n] ";
	chomp($foo = <STDIN>);
	if ($foo =~ /n/i) {
		$create_file = 0;
	} else {
		$create_file = 1;
	}
}

## If we're forcing, or they want an update...
if ( ($create_file > 0) || ($ARGS{force} > 0) ) {
	open(WRITEFILE, ">".$HOME."/.vim_update.sh") or die("Unable to open $HOME/.vim_update.sh!\n$!\n");
	## Grab the time...
	my $time = localtime();
	## Print the script into the file...
	print WRITEFILE <<EOF;
#!/bin/sh
# Updated on $time.
cd $PWD;
$SVN update --quiet;
$PWD/setup.pl --quiet --force;
EOF
	## ^^ End of inputting to the file.
	close(WRITEFILE);
	## Make it executable.
	chmod 0755, $HOME."/.vim_update.sh";
}
unless ( ($ARGS{force} > 0) || ($ARGS{quiet} > 0) ) {
	print "Do you want to start setup now? [Y/n] ";
	chomp($foo = <STDIN>);
	my %config;
	unless ($foo =~ /n/i) {
		unless (-d $HOME."/.vim") {
			mkdir $HOME."/.vim" || die("Unable to create $HOME/.vim/ to store configuration in.\n$! ");
		}
		unless (-d $HOME."/.vim/plugin") {
			system("ln -s $PWD/plugin $HOME/.vim/plugin");
		}
		my %BASE;
		$BASE{logs}		=	"filename flags";
		$BASE{apache}	=	"binary flags";
		$BASE{mysql}	=	"binary database username password flags";
		$BASE{shell}	=	"binary flags";
		foreach my $file (keys %BASE) {
			if (-f $HOME."/.vim/$file.sh") {
				open(READFILE, "<".$HOME."/.vim/$file.sh");
				while (local $_ = <READFILE>) {
					if (/^## Config:(.*?)$/) {
						my $config = $1;
						my @parts = split(/&/, $config);
						foreach my $p (@parts) {
							my ($k,$v) = split(/=/, $p);
							$config{$file}{$k} = $v;
						}
					}
				}
				close(READFILE);
			}
			foreach my $k (split(/ /, $BASE{$file})) {
				print "Please specify your ".ucfirst($file)." ".ucfirst($k)." [".$config{$file}{$k}."] ";
				chomp($foo = <STDIN>);
				if (length($foo) > 0) {
					$config{$file}{$k} = $foo;
				}
			}
			my $contents;
			if ($file eq 'logs') {
				$contents = <<EOF;
#!/bin/sh
clear;
tail -f $config{$file}{flags} $config{$file}{filename};
EOF
			} elsif ($file eq 'apache') {
				$contents = <<EOF;
#!/bin/sh
$config{$file}{binary} $config{$file}{flags};
EOF
			} elsif ($file eq 'shell') {
				$contents = <<EOF;
#!/bin/sh
$config{$file}{binary} $config{$file}{flags};
EOF
			} elsif ($file eq 'mysql') {
				$contents = <<EOF;
#!/bin/sh
$config{$file}{binary} -u$config{$file}{username} -p$config{$file}{password} $config{$file}{flags} $config{$file}{database};
EOF
			}
			$contents .= "\n## Config:";
			foreach my $k (keys %{ $config{$file} }) {
				$contents .= $k."=".$config{$file}{$k}."&";
			}
			$contents .= "\n";
			open(OUTFILE, ">".$HOME."/.vim/$file.sh");
			print OUTFILE $contents;
			close(OUTFILE);
			chmod 0755, $HOME."/.vim/$file.sh";
		}
	}
}

## Unless we're being all sneaky...
unless ($ARGS{quiet}) {
	## Let 'em know we're done!
	print "All setup, let 'er rip!\n";
}
